package com.microinvest.service.impl;
import com.microinvest.dao.EvaluateUserBehaviorMapper;
import com.microinvest.dao.TransactMapper;
import com.microinvest.entity.BuyStock;
import com.microinvest.entity.SellStock;
import com.microinvest.entity.UserStockSummary;
import com.microinvest.service.BusinessService;
import com.microinvest.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
@Transactional
public class BusinessServiceImpl implements BusinessService {

    private final TransactMapper transactMapper;

    private final EvaluateUserBehaviorMapper evaluateUserBehaviorMapper;

    @Autowired
    public BusinessServiceImpl(TransactMapper transactMapper,EvaluateUserBehaviorMapper evaluateUserBehaviorMapper){
        this.transactMapper=transactMapper;
        this.evaluateUserBehaviorMapper=evaluateUserBehaviorMapper;
    }

//    @Override
//    public Result<String> addStockData() {
//        return null;
//    }
//
//    //测试是否能得到用户stockSummary数据
//    @Override
//    public Result<String> testToGetUserStockSummary(Integer userId) {
//        UserStockSummary userStockSummary=transactMapper.getUserStockSummary(userId);
//        System.out.println(userStockSummary.getStockData());
//        return null;
//    }

    //用户购买股票
    @Override
    public Result<String> userBuyStock(int userId, String stockSymbol, double buyPrice, int quantity) {
        // 将DOUBLE价格数据转换为BigDecimal
        BigDecimal buyPriceBigDecimal = new BigDecimal(buyPrice);

        // 构造buyStock实体化类
        BuyStock buyStock = new BuyStock(userId, stockSymbol, buyPriceBigDecimal, quantity);

        // 1. 检查余额是否充足
        if (!evaluateUserBehaviorMapper.isUserBalanceEnough(userId, buyStock.getTotalAmount())) {
            return Result.error("您的余额不足");
        }

        // 2. 扣减用户余额
        evaluateUserBehaviorMapper.reduceUserBalance(userId, buyStock.getTotalAmount());

        // 3. 更新用户购买的股票数据
        Boolean isUserBuyThisStock = evaluateUserBehaviorMapper.isUserBuyThisStock(userId, stockSymbol);
        if (!isUserBuyThisStock) {
            // 如果用户没有购买过此股票，则初始化该股票数据
            evaluateUserBehaviorMapper.addStockToUser(userId, stockSymbol);
        }

        //添加用户购买的总amount
        transactMapper.addUserTotalAmount(stockSymbol,buyStock.getTotalAmount());

        // 4. 增加用户持有股票数量
        transactMapper.addStockQuantity(userId, stockSymbol, quantity);

        // 5. 添加购买记录（更新购买信息）
        transactMapper.addUserBuyStock(buyStock);

        return Result.success("购买成功");
    }

    //用户卖出股票
    @Override
    public Result<String> userSellStock(int userId, String stockSymbol, double sellPrice, int quantity) {
        // 将用户输入的卖出价格从 double 转换为 BigDecimal，以提高计算精度
        BigDecimal sellPriceBigDecimal = new BigDecimal(sellPrice);

        // 创建 SellStock 对象，封装卖出股票所需的信息（包括用户ID、股票符号、卖出价格、数量）
        SellStock sellStock = new SellStock(userId, stockSymbol, sellPriceBigDecimal, quantity);

        //返回现在持有的股票数量
        int stockTotalQuantity=transactMapper.returnOneStockTotalQuantity(userId,stockSymbol);

        //如果数量不足则返回
        if(stockTotalQuantity<quantity){
            return Result.error("您持有的股票数量不足");
        }

        // 1. 增加用户账户余额：卖出股票后，用户应当收到对应金额
        evaluateUserBehaviorMapper.addUserBalance(userId, sellStock.getTotalAmount());

        // 2. 减少用户持有的股票数量：更新数据库中用户该股票的数量
        transactMapper.reduceStockQuantity(userId, stockSymbol, quantity);

        //减少用户购买的总amount
        transactMapper.reduceUserTotalAmount(stockSymbol,sellStock.getTotalAmount());

        // 3. 记录卖出操作：将卖出信息（例如：卖出价格、数量等）保存到交易记录中
        transactMapper.addUserSellStock(sellStock);

        // 4. 返回成功结果，表示股票售出成功
        return Result.success("售出成功");
    }

    //用户收藏股票
    @Override
    public Result<String> userStarStock(int userId, String stockSymbol) {
        //判断用户是否收藏过此股票
        boolean isUserStarThisStock=evaluateUserBehaviorMapper.isUserStarThisStock(userId,stockSymbol);
        if(isUserStarThisStock){
            return Result.error("您已经收藏过此股票");
        }
        //收藏此股票
        evaluateUserBehaviorMapper.starThisStock(userId,stockSymbol);
        return Result.success("收藏成功");
    }


}
